using System;
using System.Drawing;
using System.Drawing.Imaging;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using RPGOnline.Rendering;

namespace RPGOnline.Controls.Map.Layers
{
    public class MapLayer : ICellsLayer
    {
		protected readonly Renderer m_renderer;
        private readonly Map m_map;

        public MapCell[,] Cells { get; private set; }
        public string Name { get; set; }

        public MapLayer(Renderer renderer, string name, Map map)
        {
            Name = name;
            m_renderer = renderer;
            m_map = map;
            
            Clear();
        }
		
        public void Clear()
        {
            Cells = new MapCell[m_map.MapSize.Width, m_map.MapSize.Height];

            for (int x = 0; x < m_map.MapSize.Width; x++)
            {
                for (int y = 0; y < m_map.MapSize.Height; y++)
                {
                    Cells[x, y] = new MapCell();
                }
            }
        }

        public MapCell this[int x, int y]
        {
            get { return Cells[x, y]; }
            set { Cells[x, y] = value; }
        }

	    public void DrawObstructionMap()
	    {
		    /*int minX = (int) Math.Floor(bounds.Left/m_map.TileSize.Width);
		    int minY = (int) Math.Floor(bounds.Top/m_map.TileSize.Height);
			int maxX = (int)Math.Ceiling(bounds.Right / m_map.TileSize.Width);
			int maxY = (int)Math.Ceiling(bounds.Bottom / m_map.TileSize.Height);

		    for (int x = minX; x < maxX; x++)
		    {
			    for (int y = minY; y < maxY; y++)
			    {
					MapCell mapCell = Cells[x, y];

					if (mapCell.Frames == null)
						continue;

					DrawTile(mapCell, x, y, false);
			    }
		    }*/
	    }

	    public virtual void Draw()
        {
            for (int x = 0; x < m_map.MapSize.Width; x++)
            {
                for (int y = 0; y < m_map.MapSize.Height; y++)
                {
                    MapCell mapCell = Cells[x, y];

                    if (mapCell.Frames == null)
                        continue;

                    DrawTile(mapCell, x, y);
                }
            }
        }

        protected virtual void DrawTile(MapCell tile, int x, int y, bool withTexture = true)
        {
            if (tile.Frames.Count == 0)
                return;

			tile.Frame.Draw(m_renderer, x, y, withTexture);
        }

        public override string ToString()
        {
            return Name;
        }
    }
}